/*
 * @Author: dlb
 * @Date: 2020-06-22 16:41:14
 * @LastEditTime: 2020-06-28 17:39:02
 * @LastEditors: dlb
 * @Description:
 * @FilePath: \react-training-camp\client\src\api\index.ts
 * @可以输入预定的版权声明、个性签名、空行等
 */
import axios from 'axios'
//设置AJAX请求的基准路径,如果是开发环境就指向 http://localhost:8000
axios.defaults.baseURL =
  process.env.NODE_ENV === 'production' ? '/' : 'http://47.115.46.4:9999'
//设置请求体类型为 application/json
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
axios.interceptors.request.use(
  (config) => {
    //在发送请求前把sessionStorage中的token写到请求头里
    let access_token = sessionStorage.getItem('access_token')
    config.headers = {
      Authorization: `Bearer ${access_token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(
  (response) => response.data,
  (error) => Promise.reject(error)
)
export default axios
